package cn.bdqn.controller;

import cn.bdqn.common.Config;
import cn.bdqn.model.Department;
import cn.bdqn.model.SysMenus;
import cn.bdqn.model.SysRoles;
import cn.bdqn.model.SysUsers;

import cn.bdqn.result.Result;
import cn.bdqn.result.ResultUtils;
import cn.bdqn.service.DepartmentService;
import cn.bdqn.service.SysMenusService;
import cn.bdqn.service.SysRolesService;
import cn.bdqn.service.SysUsersService;
import cn.bdqn.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Package: cn.bdqn.controller
 * @Description:
 * @Author hah
 * @Create 2021年03月20日 18时30分44秒
 */
@Controller
@RequestMapping("/api")
public class AutoController {

    /**
     * 服务对象
     */
    @Resource
    private SysUsersService sysUsersService;

    @Autowired
    SysMenusService sysMenusService;

    @Autowired
    SysRolesService sysRolesService;

    @Autowired
    DepartmentService departmentService;

    /**
     * @return java.lang.String
     * @Author: hah
     * @Date: 2021-03-23 00:03:00
     * @Description: 进入登入页面
     **/
    @RequestMapping("/Login")
    public String Login() {
        System.out.println("进入登入页面");
        return "login";
    }
    
    /**
     * @Author: hah
     * @Date: 2021-03-23 12:03:38
     * @Description: 进入主页面
     * @return java.lang.String
     **/
    @RequestMapping("/main")
    public String main(Model model,HttpServletRequest request) {
        try {
            // 获取登录的权限id
            HttpSession session = request.getSession();
            Integer attribute = (Integer) session.getAttribute(Config.SESSION_USER_ROLES_ID);
            List<SysMenus> sysMenus = sysMenusService.getMenuList(attribute);
            model.addAttribute("sysMenus", sysMenus);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("进入主页面");
        return "main";
    }

    /**
     * @return java.lang.String
     * @Author: hah
     * @Date: 2021-03-23 00:03:22
     * @Description: 登入方法
     **/
    @RequestMapping("doLogin")
    public String doLogin(SysUsers sysUsers, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("\t===用户名： " + sysUsers.getAccount() + " ===");
        System.out.println("\t===密码： " + sysUsers.getPassword() + " ===");
        Result<SysUsers> result = sysUsersService.dologin(sysUsers);
        if ("200".equals(result.getCode())) {
            HttpSession session = request.getSession();
            //保存对象
            SysUsers usersession = result.getData();
            session.setAttribute(Constants.USER_SESSION,usersession);

            try {
                response.sendRedirect(request.getContextPath() + "/api/main");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //登入失败，返回登入页面
        model.addAttribute("error", result.getMsg());
        model.addAttribute("sysUser", sysUsers);
        return "login";
    }

    /**
     * @Author: hah
     * @Date: 2021-03-23 15:03:27
     * @Description: 注销
     * @param session   获取session清空
     * @return java.lang.String
     **/
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute(Constants.USER_SESSION);
        return "login";
    }

    /**
     * @Author: hah
     * @Date: 2021-03-25 08:03:37
     * @Description: 加载用户菜单
     * @param userId
     * @return cn.bdqn.result.Result<java.util.List<cn.bdqn.model.SysUsers>>
     **/
    @ResponseBody
    @GetMapping("/getMenuList.json")
    public Result<List<SysMenus>> getMenuList(@RequestParam("userId")Integer userId){
        return sysMenusService.queryAllByRolesIdToResult(userId);
    }

    /**
     * @Author: hah
     * @Date: 2021-03-25 23:03:30
     * @Description: 进入添加页面时，加载用户类型
     * @return java.lang.String
     **/
    @ResponseBody
    @GetMapping("/auth/getRoleList.json")
    public Result<List<SysRoles>> getRoleList() {
        return sysRolesService.getResultAll();
    }

    /**
     * @Author: hah
     * @Date: 2021-03-28 16:03:42
     * @Description:   查询所有就诊科室表
     * @return java.lang.String
     **/
    @ResponseBody
    @RequestMapping("/department/getDepartmentList.json")
    public Result<List<Department>>  getAll() {
        return ResultUtils.returnDataSuccess(departmentService.getAlldepartment());
    }
    
    

}
